<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

	<script type="text/javascript" src="/plugins/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/plugins/bootstrap-fileinput/js/locales/zh.js"></script>
	<link rel="stylesheet" type="text/css" href="/plugins/bootstrap-fileinput/css/fileinput.css">
    <script>
		$(function(){
			contentList();
			$("#select_add_id").select2();
			$("#select_update_id").select2();
		})
		function contentList(){
			$.ajax({
				url:"/content/contentList",
				data:{},
				dataType:"json",
				type:"post",
				async:false,
				success:function (result) {
					if (result.code == 10000){
						var list = result.data;
						var str ='';
						for (var i = 0;i<list.length;i++){
							str+='<tr>'
							str+='<td><input type="checkbox" name="box" value="'+list[i].id+'"></td>'
							str+='<td>'+list[i].id+'</td>'
							str+='<td>'+list[i].categoryId+'</td>'
							str+='<td>'+list[i].title+'</td>'
							str+='<td>'+list[i].url+'</td>'
							str+='<td> <img src="'+list[i].pic+'"  width="100px" height="50px"> </td>'
							str+='<td>'+list[i].sortOrder+'</td>'
1
							if (list[i].status == 1){
								str+=' <td>有效</td>'
							}else {
								str+=' <td>无效</td>'
							}
							str+='<td class="text-center">'
							str+='<button type="button" onclick="toUpdate('+list[i].id+')" className="btn bg-olive btn-xs" data-toggle="modal"data-target="#editModal">修改 </button>'
							str+='</td>'
							str+='</tr>'
						}
						$("#contentList").html(str);
					}
				},error:function (error) {
					console.log(error)
				}
			})
		}

		function toAdd() {
			$.ajax({
				url:"/content/toAdd",
				data:{},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					var list = result.data;
					var str  = '';
					str += '<option>请选择</option>'
					for (var i = 0; i < list.length; i++) {
						str += '<option  value="'+list[i].id+'">'+list[i].name+'</option>'
					}

					$("#select_add_id").html(str);
				},
				error:function (e) {
					console.log(e);
				}
			})
		}

		$(function () {
			$('#testfile_add').fileinput({
				language: 'zh',
				uploadUrl: '/file/upLoadFile',
				maxFileCount : 3,
				allowedFileExtensions : ['jpg', 'gif', 'png'],
				maxFileSize : 1048576,
				enctype : 'multipart/form-data'
			});
		})
		$(function () {
			$('#testfile_add').on('fileuploaded',function (event,data,previewId,index) {
				var Img = data.response.data;
				$("#img_add_id").val(Img);
			})
		})

		$(function () {
			$('#testfile_update').fileinput({
				language: 'zh',
				uploadUrl: '/file/upLoadFile',
				maxFileCount : 3,
				allowedFileExtensions : ['jpg', 'gif', 'png'],
				maxFileSize : 1048576,
				enctype : 'multipart/form-data',
				initialPreviewAsData: true,//是否初始预览图片
				initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
				initialPreview: '/file/download?filename='+$("[name=pic_edit]").val(),//图片回显(初始预览图片地址)
			});
		})
		$(function () {
			$('#testfile_update').on('fileuploaded',function (event,data,previewId,index) {
				var Img = data.response.data;
				$("#img_update_id").val(Img);
			})
		})


		//增加方法
		function insertInfo() {
			//是否有效
			var is_default = $("#box").prop("checked");
			var status = "0";
			if (is_default){
				status = "1";
			}

			$.ajax({
				url:"/content/insertInfo",
				data: {
					categoryId:$("[name=categoryId]").val(),
					title:$("#title").val(),
					url:$("#url").val(),
					pic:$("[name=pic]").val(),
					status:status,
					sortOrder:$("#sortOrder").val()
				},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					if (result.code == 10000){
						window.location.reload();
					}
				},
				error:function (e) {
					console.log(e);
				}
			})
		}

		function toUpdate(id){
			$.ajax({
				url:"/content/toUpdate",
				data: {id:id},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					if (result.code == 10000){
					var rows = result.rows;
					var data = result.data;
					var str ='';
					for (var i = 0;i<rows.length;i++){
						str+='<option value="'+rows[i].id+'" >'+rows[i].name+'</option>'
					}
						$("#select_update_id").html(str)
						$("#select_update_id").val(data.categoryId).trigger('change');
						$("#id_id").val(data.id)
						$("#title_id").val(data.title)
						$("#url_id").val(data.url)
						$("#sortOrder_id").val(data.sortOrder)
						if(data.status == 1){
							$("#box1").prop("checked",true)
						}else {
							$("#box1").val(0)
						}
						$("#img_update_id").val(data.pic)
					}
				},
				error:function (e) {
					console.log(e);
				}
			})
		}

		function updateInfo(){
			//是否有效
			var is_default = $("#box1").prop("checked");
			var status = "0";
			if (is_default){
				status = "1";
			}
			$.ajax({
				url:"/content/updateInfo",
				data: {
					status:status,
					categoryId:$("[name=categoryId_edit]").val(),
					title:$("[name=title_edit]").val(),
					url:$("[name=url_edit]").val(),
					pic:$("[name=pic_edit]").val(),
					sortOrder:$("[name=sortOrder_edit]").val(),
					id:$("[name=id_edit]").val()
				},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					if (result.code == 10000){
					contentList();
					}
				},
				error:function (e) {
					console.log(e);
				}
			})
		}

		function deleteInfo(){
			var ids = [];
			var boxs = document.getElementsByName("box");
			for (var i = 0; i < boxs.length; i++) {
				if(boxs[i].checked){
					ids.push(boxs[i].value)
				}
			}
			$.ajax({
				url:"/content/deleteById",
				data:{ids:ids},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					if (result.code == 10000){
						contentList();
					}
				},
				error:function (e) {
					console.log(e);
				}
			})
		}
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->

                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" onclick="toAdd()" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteInfo()" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">

                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>
									      <th class="sorting">图片</th>
									      <th class="sorting">排序</th>
									      <th class="sorting">是否有效</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="contentList">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

  <!-- 增加窗口 -->
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel_add" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel_add">广告增加</h3>
			  </div>
			  <div class="modal-body">
				  <form id="contentInsertForm">
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="select_add_id" name="categoryId">

							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input type="text" class="form-control" placeholder="标题" id="title" >  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input type="text" class="form-control" placeholder="URL" id="url" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input type="text" class="form-control" placeholder="排序" id="sortOrder" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <label class="control-label" for="testfile_add">上传文件</label>
									  <input type="text"  id="img_add_id" name="pic">
									  <input type="file" id="testfile_add" name="image" multiple />
								  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" id="box" class="icheckbox_square-blue" >
						  </td>
					  </tr>
				  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal"  onclick="insertInfo()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
  <!-- 回显窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="select_update_id" name="categoryId_edit">

							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td>
							  <input type="text" class="form-control" placeholder="标题" name="title_edit" id="title_id" >
							  <input type="hidden" class="form-control" placeholder="标题" name="id_edit" id="id_id" >
						  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input type="text" class="form-control" placeholder="URL" name="url_edit" id="url_id">  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input type="text" class="form-control" placeholder="排序" name="sortOrder_edit" id="sortOrder_id" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td></td>
									  <td>
										  <label class="control-label" for="testfile_update">上传文件</label>
										  <input type="text"  id="img_update_id" name="pic_edit">
										  <input type="file" id="testfile_update" name="image" multiple />
									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" id="box1" class="icheckbox_square-blue" >
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button type="button" onclick="updateInfo()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
</div>

</body>

</html>